<?php
/**
 * 券校验可用类
 */

namespace App\Kuafu\Coupon\Service;

use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Coupon\Enum\CouponCodeEnum;
use App\Kuafu\Coupon\Exceptions\CouponException;
use App\Kuafu\CustomerCoupon\Enum\CodeEnum;
use App\Kuafu\CustomerCoupon\Model\CustomerCouponModel;
use App\Kuafu\Shop\Service\ShopService;
use Illuminate\Support\Facades\Log;

class EnableCouponService extends BaseService
{
    /**
     * @var array 待校验的券列表
     */
    private array $needCheckCouponList = [];

    /**
     * @var array 可用列表
     */
    private array $enableList = [];

    public function getEnableList()
    {
        return $this->enableList;
    }

    /**
     * @var array 不可用列表
     */
    private array $unableList = [];

    public function getUnableList(): array
    {
        return $this->unableList;
    }

    /**
     * @param array $enableList
     */
    public function addEnableList(array $enableList, bool $replace = false): void
    {
        if ($replace) {
            $this->enableList = $enableList;
        } else {
            $this->enableList[] = $enableList;
        }
    }

    /**
     * @param array $unableList
     */
    public function addUnableList(array $unableList, bool $replace = false): void
    {
        if ($replace) {
            $this->unableList = $unableList;
        } else {
            $this->unableList[] = $unableList;
        }
    }

    /**
     * @return array
     */
    public function getNeedCheckCouponList(): array
    {
        return $this->needCheckCouponList;
    }

    /**
     * @param array $needCheckCouponList
     */
    public function setNeedCheckCouponListByCodes(array $needCheckCouponList): void
    {
        $list = CustomerCouponModel::query()
                                   ->whereIn('couponCode', $needCheckCouponList)
                                   ->with('couponShopRel')
                                   ->get()
                                   ->each(function ($item) {
                                       $inShopList = [];
                                       foreach ($item->couponShopRel as $couponShop) {
                                           $inShopList[] = $couponShop->shopId;
                                       }
                                       $item->inShopList = $inShopList;
                                       unset($item->couponShopRel);
                                   })
                                   ->toArray();

        $this->needCheckCouponList = $list;
    }


    /**
     * 初始化校验
     *
     * @return $this
     */
    public function initCheck()
    {
        return $this->checkTime();
    }

    /**
     * 校验 POS 查看券信息的接口
     */
    public function checkPosCouponInfo()
    {
        $this->initCheck()->checkShopId();
    }

//    /**
//     * 校验购物车用券列表
//     */
//    public function checkPosShoppingCartCouponList()
//    {
//        $this->initCheck()->checkShopId();
//    }


    /***券校验规则***/

    /**
     * 校验券有效期
     *
     * @return $this
     */
    public function checkTime()
    {
        foreach ($this->needCheckCouponList as $value) {
//            //使用有效期  1 固定时间 2 相对时间
//            if ($value['useTimeType'] == 1) {
//                //固定时间
//                $startAt = $value['startAt'];
//                $endAt   = $value['endAt'];
//            } else {
//                //相对时间
//                $startAt = strtotime($value['createdAt']) + $value['startAt'] * 86400;
//                $endAt   = strtotime($value['createdAt']) + ($value['startAt'] + $value['endAt']) * 86400;
//            }

            $startAt = $value['startAt'];
            $endAt   = $value['endAt'];

            if (time() <= $startAt || time() >= $endAt) {
                $value['errCause'] = '券不在有效期内';
                $this->addUnableList($value);
                continue;
            }

            $this->addEnableList($value);
        }
        return $this;
    }

    /**
     * 校验适用门店-简易版本
     *
     * @return $this
     */
    public function checkShopId()
    {
        $shopInfo = ShopService::getShopInfo($this->shopId);

        $couponList       = $this->enableList;
        $this->enableList = [];

        foreach ($couponList as $value) {
            switch ($value['shopType']) {
                case CodeEnum::COUPON_SHOP_TYPE_JOIN:
                    // TODO 过滤非加盟
                    break;
                case CodeEnum::COUPON_SHOP_TYPE_DIRECTLY:
                    // TODO 过滤非直营
                    break;
                case CodeEnum::COUPON_SHOP_TYPE_PART:
                    // 过滤部分店铺
                    if (!in_array($this->shopId, $value['inShopList'])) {
                        $value['errCause'] = '券不在适用门店内';
                        $this->addUnableList($value);
                        continue 2;
                    }
                    break;
                case CodeEnum::COUPON_SHOP_TYPE_ALl :
            }

            $this->addEnableList($value);

        }
        return $this;
    }
}